{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start\n",
      "[[   1    2    3    4    5    6]\n",
      " [   5    1    0  920  102    1]\n",
      " [   5    0    3  950  102    0]\n",
      " [   4    2    1  930  100    0]\n",
      " [   4    1    4  960  100    0]\n",
      " [   4    0    7  990  100    0]\n",
      " [   3    4    0  960  102    0]\n",
      " [   3    3    3  990  102    0]\n",
      " [   3    2    6 1020  102    0]\n",
      " [   3    1    9 1050  102    0]\n",
      " [   3    0   12 1080  102    0]\n",
      " [   2    5    1  970  100    0]\n",
      " [   2    4    4 1000  100    0]\n",
      " [   2    3    7 1030  100    0]\n",
      " [   2    2   10 1060  100    0]\n",
      " [   2    1   13 1090  100    0]\n",
      " [   2    0   16 1120  100    0]\n",
      " [   1    7    0 1000  102    0]\n",
      " [   1    6    3 1030  102    0]\n",
      " [   1    5    6 1060  102    0]\n",
      " [   1    4    9 1090  102    0]\n",
      " [   1    3   12 1120  102    0]\n",
      " [   1    2   15 1150  102    0]\n",
      " [   1    1   18 1180  102    0]\n",
      " [   1    0   21 1210  102    0]\n",
      " [   0    8    1 1010  100    0]\n",
      " [   0    7    4 1040  100    0]\n",
      " [   0    6    7 1070  100    0]\n",
      " [   0    5   10 1100  100    0]\n",
      " [   0    4   13 1130  100    0]\n",
      " [   0    3   16 1160  100    0]\n",
      " [   0    2   19 1190  100    0]\n",
      " [   0    1   22 1220  100    0]\n",
      " [   0    0   25 1250  100    0]]\n"
     ]
    }
   ],
   "source": [
    "class Car:\n",
    "    price=0.0\n",
    "    capacity=0.0\n",
    "\n",
    "    def __init__(self, price, capacity):\n",
    "        self.capacity = capacity\n",
    "        self.price = price\n",
    "\n",
    "import math\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "\n",
    "\n",
    "def min_capacity(car_list):\n",
    "    min_c = 10000000000;\n",
    "    for car in car_list:\n",
    "        if (min_c > car.capacity):\n",
    "            min_c = car.capacity\n",
    "    return min_c\n",
    "\n",
    "\n",
    "def price(count_list, car_list):\n",
    "    price_result = 0;\n",
    "    for index, car in enumerate(car_list):\n",
    "        price_result += car.price * count_list[index]\n",
    "    return price_result\n",
    "\n",
    "\n",
    "def capacity(count_list, car_list):\n",
    "    capacity_result = 0;\n",
    "    for index, car in enumerate(car_list):\n",
    "        capacity_result+= car.capacity * count_list[index]\n",
    "    return capacity_result\n",
    "\n",
    "\n",
    "def main():\n",
    "    print(\"start\")\n",
    "\n",
    "    totalPerson = 100\n",
    "    car3= Car(50, 4)\n",
    "    car2 = Car(120, 12)\n",
    "    car1 = Car(160, 18)\n",
    "\n",
    "    car_list = []\n",
    "    car_list.append(car1)\n",
    "    car_list.append(car2)\n",
    "    car_list.append(car3)\n",
    "\n",
    "    max_count = {}\n",
    "    for car in car_list:\n",
    "        max_count[car] = math.ceil(totalPerson / car.capacity)\n",
    "\n",
    "    n1 = 0\n",
    "    n2 = 0\n",
    "    n3 = 0\n",
    "\n",
    "    max_n1 = math.ceil(totalPerson / car1.capacity)\n",
    "    max_n2 = math.ceil(totalPerson / car2.capacity)\n",
    "    max_n3 = math.ceil(totalPerson / car3.capacity)\n",
    "\n",
    "    # searchSpace = np.array([['1','2','3','费用','容量','最划算？']])\n",
    "    searchSpace = np.array([[1,2,3,4,5,6]])\n",
    "\n",
    "    for n1 in range(max_n1, -1, -1):\n",
    "        for n2 in range(max_n2, -1, -1):\n",
    "            for n3 in range(max_n3, -1, -1):\n",
    "                if ((totalPerson < (n1 * car1.capacity + n2 * car2.capacity + n3 * car3.capacity)\n",
    "                     or totalPerson == (n1 * car1.capacity + n2 * car2.capacity + n3 * car3.capacity))\n",
    "                        and totalPerson + min_capacity(car_list) > (\n",
    "                                n1 * car1.capacity + n2 * car2.capacity + n3 * car3.capacity)):\n",
    "                    p = price([n1, n2, n3], car_list)\n",
    "                    searchSpace =np.append(searchSpace, [[n1, n2, n3, p, capacity([n1, n2, n3], car_list),0]],axis=0 )\n",
    "\n",
    "    searchSpace[1:,5] = (searchSpace[1:,3] == min(searchSpace[1:,3]))\n",
    "    print(searchSpace)\n",
    "\n",
    "def main1():\n",
    "    print(\"start\")\n",
    "\n",
    "    totalPerson = 100\n",
    "    car1 = Car(50, 4)\n",
    "    car2 = Car(120, 12)\n",
    "    car3 = Car(160, 18)\n",
    "\n",
    "    car_list = []\n",
    "    car_list.append(car1)\n",
    "    car_list.append(car2)\n",
    "    car_list.append(car3)\n",
    "\n",
    "    max_count = {}\n",
    "    for car in car_list:\n",
    "        max_count[car] = math.ceil(totalPerson / car.capacity)\n",
    "\n",
    "    n1 = 0\n",
    "    n2 = 0\n",
    "    n3 = 0\n",
    "\n",
    "    max_n1 = math.ceil(totalPerson / car1.capacity)\n",
    "    max_n2 = math.ceil(totalPerson / car2.capacity)\n",
    "    max_n3 = math.ceil(totalPerson / car3.capacity)\n",
    "\n",
    "    searchSpace = []\n",
    "\n",
    "    for n1 in range(max_n1, -1, -1):\n",
    "        for n2 in range(max_n2, -1, -1):\n",
    "            for n3 in range(max_n3, -1, -1):\n",
    "                if ((totalPerson < (n1 * car1.capacity + n2 * car2.capacity + n3 * car3.capacity)\n",
    "                     or totalPerson == (n1 * car1.capacity + n2 * car2.capacity + n3 * car3.capacity))\n",
    "                        and totalPerson + min_capacity(car_list) > (\n",
    "                                n1 * car1.capacity + n2 * car2.capacity + n3 * car3.capacity)):\n",
    "                    searchSpace.append([n1, n2, n3, price([n1, n2, n3], car_list)])\n",
    "\n",
    "    a = np.asarray(searchSpace)\n",
    "    data = a[a[:, 2] == 0][:, [0, 1, 3]]\n",
    "    print(data)\n",
    "\n",
    "    # draw\n",
    "    x = data[:, 0]\n",
    "    y = data[:, 1]\n",
    "    p = data[:, 2]\n",
    "    #\n",
    "    ax = plt.subplot(111)\n",
    "    ax.scatter(x, y, s = p - 2 / 3 * min(p), c=p - min(p))\n",
    "    ax.set_xlabel('car1')\n",
    "    ax.set_ylabel('car2')\n",
    "    for i,xx in enumerate(x):\n",
    "        plt.text(x[i], y[i] + 0.2, '%.0f' % p[i], ha='center', va='bottom', fontsize=11)\n",
    "    # ax.set_zlabel('price')\n",
    "    plt.show()\n",
    "\n",
    "    fig = plt.figure()  # 创建一张图片\n",
    "    ax3d = Axes3D(fig)\n",
    "    xx, yy = np.meshgrid(x,y)\n",
    "    ax3d.plot_surface(xx,yy,f(xx,yy, data),rstride=1,cstride=1,cmap=plt.cm.spring)#cmap还可以是summer autumn winter也可以自己配置\n",
    "    plt.show()\n",
    "\n",
    "def f(xx,yy, data):\n",
    "    zz = np.array([xx.ravel(), yy.ravel()])\n",
    "    # print(zz.T.shape)\n",
    "    # print(data[:,[0,1]].T.shape)\n",
    "    # print(zz.T)\n",
    "    result = np.array([])\n",
    "    print(result.shape)\n",
    "    for i in range(0, zz.shape[1]):\n",
    "        filter = (data[:, [0, 1]] == zz.T[i])[:, 0] & (data[:, [0, 1]] == zz.T[i])[:, 1]\n",
    "        #     print(filter)\n",
    "        if (filter.any()):\n",
    "            #         print(data[filter][0,2])\n",
    "            result = np.append(result, data[filter][0, 2])\n",
    "        #         print(result)\n",
    "        else:\n",
    "            result = np.append(result, 0)\n",
    "    print(result.shape)\n",
    "    result.shape = (9, 9)\n",
    "    print(result)\n",
    "    return result\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
